﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using Common;
using LogicAndDB;
using System.Net.Mail;
using System.Net.Security;
using System.Net;
namespace AccountManagement.code
{
    /// <summary>
    /// 处理邮件激活的逻辑
    /// </summary>
    public class ActivationReq
    {

        HttpRequest _currentReq;
        public static ActivationReq ParseFromCurrentRequest()
        {
            if (Util.IsParamInQueryString(RequestParameter.email.ToString()) &&
                Util.IsParamInQueryString(RequestParameter.key.ToString()) &&
                Util.IsParamInQueryString(RequestParameter.time.ToString()))
            {
                ActivationReq req = new ActivationReq();
                req._currentReq = HttpContext.Current.Request;
                return req;
            }
            else
            {
                return null;
            }
            
        }
        #region 枚举

        /// <summary>
        /// 表示Request中的参数
        /// device : 接口参数  key: url中邮箱激活标识 
        /// email : url中用户名 time : url中注册时间
        /// jsonRequest : 前后台通信请求
        /// </summary>
        public enum RequestParameter
        {
            device, key, email, time, jsonRequest
        }

        public enum KeyFlag
        {
            Activate
        }
        #endregion
        public string _requestParameter;

        //根据URL中'email'参数获取用户名
        public string UserName
        {
            get { return HttpUtility.UrlDecode(_currentReq.QueryString[RequestParameter.email.ToString()]); }
        }

        //根据URL中‘key’参数获取邮箱激活标识
        public string Key
        {
            get { return HttpUtility.UrlDecode(_currentReq.QueryString[RequestParameter.key.ToString()]); }
        }
        
        const int TimeoutMinutes = 120;
        /// <summary>
        /// 检验时间是否超时
        /// </summary>
        /// <returns></returns>
        private bool TimeCheck()
        {

            string timeStr = _currentReq.QueryString[RequestParameter.time.ToString()];
            string requesstUrlTime = System.Web.HttpUtility.UrlDecode(timeStr);

            if (requesstUrlTime == "")
                return false;

            try
            {
                DateTime urlTime = DateTime.Parse(requesstUrlTime);
                if (urlTime > DateTime.Now)
                    return true;
                else
                    return false;
            }
            catch { return false; }
        }

        public CUser.ActivationResult Do()
        {
            CUser user = CUser.GetUserByMail(UserName);
            if (user == null)
                return CUser.ActivationResult.NoExistUser;
            if (TimeCheck())                      //超时判断
                return CUser.ActivationResult.OverTime;
            if (Key == user.ActivationKey)
                return CUser.ActivationResult.Pass;
            
            return CUser.ActivationResult.ActivateFial;
        }
       
       
    
    }

}